<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

final class Company extends BIT_Model
{
	public static $class_name = __CLASS__;
	
	protected $id;
	
	protected $name;
	
	protected $orgnr;
	
	protected $address;
	
	protected $short_desc;
	
	protected $phone;
	
	protected $homepage;
	
	protected $email;
	
	protected $search_tags;
	
	protected $branch;
	
	protected $lastchange;

	protected $lastchangeby;
	
	protected $slug;
	
	private $db = "";
	
	function __construct()
	{
		parent::__construct();
	}
	
	public static function getNestedBranches()
	{
		$db = get_instance()->db;
		$branches = array();
		$parents = $db->where('parent', 0)->order_by('name', 'asc')->get('branches')->result_array();
		$branch = array();
		foreach($parents as $parent)
		{
			$children = $db->where('parent', $parent['id'])->order_by('name', 'asc')->get('branches')->result_array();
			foreach ($children as $child)
			{
				$branches[$parent['name']]['name'][] = $child['name'];
				$branches[$parent['name']]['id'][] = $child['id'];
			}
		}
		return $branches;
	}
	
	public function saveCompany($data)
	{
		if(!$data)
		{
			return false;
		}
		foreach($data as $key => $value)
		{
			$this->$key = $value;
		}
		
		$this->save();
		
		return true;
	}
}